<?php
/**
 * Author: 式神(vashi)
 * email: 289650682@qq.com
 * Date: 2019/3/13 15:10
 * FileName: Position.php
 */

namespace app\cms\controller;


use app\common\controller\Common;

class Position extends Common
{
    protected $middleware = ['Auth'];

    protected function initialize()
    {
        parent::initialize();
        $this->db = new \app\cms\model\Position();
    }

    public function index(){
        $input=input('get.');
        $input['rows'] = isset($input['rows']) ? $input['rows'] : config('rows');
        $input['page'] = isset($input['page']) ? $input['page'] : 1;

        $map = $this->getMap($input);
        $order = $this->getOrder($input);
        $res['total'] = $this->db->where($map)->order($order)->count();
        $data= $this->db->where($map)->order($order)->limit($input['rows'])->page($input['page'])->select();
		foreach($data as $v){
			if($v['catid']>0){
				$cate = (new \app\cms\model\Category())->find($v['catid']);
				$v['catname']=$cate['name'];
			}else{
				$v['catname']='全部';
			}
		}
        $res['rows'] =$data;
        $res['page'] =$input['page'];
        $d=[
            "code"=>0,
        ];
        $d['data']=$res;
        return json($d);
    }
    //添加
    public function add(){
        $res=$this->db->add(input('post.'));
        return json($res);
    }
    //编辑
    public function edit(){
        $res=$this->db->edit(input('post.'));
        return json($res);
    }
    //删除
    public function del(){
        $res=$this->db->del(input('post.'));
        return json($res);
    }
	
	//栏目树
    public function catetree(){
        $get = input('get.');
        $cate = (new \app\cms\model\Category())->select();
        $arrid=[];
        if($get['id']!=0){
            $arrid=getChildsId($cate,$get['id']);
            array_push($arrid,$get['id']);
        }
        $data=data_tree($cate,'pid',$get['pid'],$arrid);
        array_unshift($data,['key'=>0,'title'=>'所有栏目','value'=>0]);
        $d=[
            "code"=>0,
        ];
        $d['data']=$data;
        return json($d);
    }
}